﻿using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Calligraphy.CalligraphyData
{
    static class DataServiceContextHelper
    {
        private static CalligraphyEntities _context;

        public static CalligraphyEntities DataContext
        {
            get
            {
                return _context;
            }
        }

        static DataServiceContextHelper()
        {
            //_context = new CalligraphyEntities(new Uri("http://113.108.163.188:8010/CalligraphyDataServer0123/WcfDataService.svc/"));
            //_context = new CalligraphyEntities(new Uri("http://172.18.49.45/Calligraphy/WcfDataService.svc/"));
            _context = new CalligraphyEntities(new Uri("http://113.108.163.188:1234/CalligraphyDataServer0222/WcfDataService.svc/"));
        
        }

       
    }

    public static class ExtensionMethods
    {
        public static async Task<IEnumerable<T>> ExecuteAsync<T>(this DataServiceQuery<T> query)
        {
            return await Task.Factory.FromAsync<IEnumerable<T>>(query.BeginExecute(null, null), query.EndExecute);
        }

        public static async Task<IEnumerable<TResult>> ExecuteAsync<TResult>(this DataServiceContext context,
                                                                             Uri requestUri)
        {
            return await Task.Factory.FromAsync<IEnumerable<TResult>>(context.BeginExecute<TResult>(requestUri, null, null),
                                                             executeAsyncResult =>
                                                             {
                                                                 List<TResult> executeResult =
                                                                     context.EndExecute<TResult>(executeAsyncResult)
                                                                         .ToList();
                                                                 return executeResult;
                                                             });
        }        
    }

    
}
